<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
    <link rel="stylesheet" href="${pageContext.request.contextPath}/js/layui/css/layui.css" media="all">


</head>
<body>
<br>
<form class="layui-form" action="">
    <%--商品名称--%>
    <div class="layui-form-item">
        <label class="layui-form-label">商品名称<span style="color: red; font-size: larger"> *</span></label>
        <div class="layui-input-block">
            <input type="text" id="goNm" style="width: 350px" name="username" value="" lay-verify="required" lay-reqtext="商品名称是必填项，岂能为空？" placeholder="请输入" autocomplete="off" class="layui-input">
        </div>
    </div>
    <%--商品价格--%>
    <div class="layui-form-item">
        <label class="layui-form-label">商品价格<span style="color: red; font-size: larger"> *</span></label>
        <div class="layui-input-block">
            <input type="text" id="goPri" style="width: 350px" name="username" lay-verify="required|price" lay-reqtext="商品价格是必填项，岂能为空？" placeholder="请输入" autocomplete="off" class="layui-input">
        </div>
    </div>
    <%--配送费--%>
    <div class="layui-form-item">
        <label class="layui-form-label">配送费<span style="color: red; font-size: larger"> *</span></label>
        <div class="layui-input-block">
            <input type="text" id="goDismoney" lay-verify="required|deliveryFee" style="width: 350px" name="password" lay-reqtext="配送费是必填项，岂能为空？" placeholder="请输入" autocomplete="off" class="layui-input">
        </div>
    </div>
    <%--商品描述--%>
    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">商品描述<span style="color: red; font-size: larger"> *</span></label>
        <div class="layui-input-block">
            <textarea id="goDp" lay-verify="title" placeholder="请输入内容" class="layui-textarea"></textarea>
        </div>
    </div>
    <%--表单提交--%>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button type="submit" class="layui-btn" lay-submit="" lay-filter="demo1">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>

<script src="${pageContext.request.contextPath}/assets/js/core/jquery.min.js"></script>
<script src="${pageContext.request.contextPath}/js/layui/layui.js"></script>


<script>
    var goId = ${param.goId}; //获得 schoolManager.jsp页面传递过来的参数值

    var param = {
        goId:goId
    }
    var url = "/goods/selectGoodsByGoId";
    //回显院校信息
    $.get(url,param,function (data){
        $("#goNm").val(data.goods.goNm);
        $("#goPri").val(data.goods.goPri);
        $("#goDismoney").val(data.goods.goDismoney);
        $("#goDp").val(data.goods.goDp);


    })

    layui.use(['form', 'layedit', 'laydate'], function(){
        var form = layui.form
            ,layer = layui.layer
            ,layedit = layui.layedit
            ,laydate = layui.laydate;

        form.render();

        //日期
        laydate.render({
            elem: '#date'
        });
        laydate.render({
            elem: '#date1'
        });

        //创建一个编辑器
        var editIndex = layedit.build('LAY_demo_editor');

        //自定义表单验证规则 （input输入框中的属性 lay-verify 的值与下面对应）
        // required 内置的校验 输入框内容不能为空
        //title 指标题验证，pass 指密码验证，可以自定义正则表达式，confirmPass 指两次密码相同验证
        form.verify({
            title: function(value){
                if(value.length < 5){
                    return '商品描述至少得5个字符啊';
                }
            }
            ,pass: [
                /^[\S]{6,12}$/
                ,'密码必须6到12位，且不能出现空格'
            ]
            ,content: function(value){
                layedit.sync(editIndex);
            }
            ,price: [           //定义商品价格的正则表达式
                /(^[1-9]\d*(\.\d{1,2})?$)|(^0(\.\d{1,2})?$)/
                ,'商品价格必须是正整数或者保留两位小数'
            ]
            ,deliveryFee: [           //定义配送费的正则表达式
                /(^[1-9]\d*(\.\d{1,2})?$)|(^0(\.\d{1,2})?$)/
                ,'配送费必须是正整数或者保留两位小数'
            ]
            ,confirmPass:function(value){
                if($('input[name=password]').val() !== value)
                    return '两次密码输入不一致！';
            }
        });

        //监听表单提交
        form.on('submit(demo1)', function(data){
            layer.confirm('确定提交吗？', function(index){ //弹出确定提交弹窗
                var param = {
                  goId:goId,
                  goNm:$("#goNm").val(),
                  goPri: $("#goPri").val(),
                  goDismoney: $("#goDismoney").val(),
                  goDp: $("#goDp").val(),
                  _method: 'put'
                }
                /*发送修改请求*/
                var url = "/goods/updateGoodsInfoByGoId";
                $.post(url,param,function (data){
                    if(data.code == 1001){      //修改成功跳转到 商品信息管理页面
                    //    layer.msg(data.msg);
                        layer.closeAll();       //关闭确认提交的弹出
                        layer.open({
                            type: 1
                            ,content: '<div style="padding: 20px 100px;">'+ data.msg +'</div>'
                            ,btn: '确定'
                            ,btnAlign: 'c' //按钮居中
                            ,shade: 0 //不显示遮罩
                            ,yes: function(){
                            //    layer.closeAll();
                                top.location='goodsManager.jsp';  //点击确定后跳转到 商品信息管理页面
                            }
                        });


                    }else if(data.code == 1002){
                        layer.msg(data.msg);
                    }
                })
            });

            return false;
        });

        //表单取值
        layui.$('#LAY-component-form-getval').on('click', function(){
            var data = form.val('example');
            alert(JSON.stringify(data));
        });

    });
</script>
</body>
</html>
